import Toast from "../../../../miniprogram_npm/@vant/weapp/toast/toast"
const $api = require('../../../../request/api').API;
// pages2/pages/person/monthBill/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    allList:[],
    list:[],
    totalPrice:"",
    nums:"",
    startIndex:0,
    endIndex:0,
    num:8,
    payType:6,
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },
  getData(){
    $api.listWaitClearing({payType:this.data.payType}).then(res=>{
      this.setData({
        allList:res.data.data,
        startIndex:0,
        endIndex:0,
      })
      this.getTotalPriceNums()
      this.filterData()
      wx.pageScrollTo({
        scrollTop:0
      })
    })
    
  },
  filterData(){
    let {list,allList,startIndex,endIndex,num} = this.data
    startIndex = endIndex
    if(endIndex+num>=allList.length){
      endIndex = allList.length
    }else{
      endIndex = endIndex+num
    }
    const list2 = allList.slice(startIndex,endIndex)
    this.setData({
      startIndex,
      endIndex,
      list:!startIndex?list2:list.concat(list2)
    })
  },
  getTotalPriceNums(){
    const {allList} = this.data
    const totalPrice = allList.reduce((a,b)=>a+b.amount,0)
    const nums = allList.reduce((a,b)=>a+b.number,0)
    this.setData({
      totalPrice:parseFloat(totalPrice.toFixed(2)),
      nums,
    })
  },
  toBillOrder(e){
    const {id,payType,orderIds} = e.currentTarget.dataset.item
    wx.navigateTo({
      url: '../billOrder/index?businessId='+id+'&payType='+payType+'&orderIds='+orderIds,
    })
  },
  modifyPayType(e){
    const {type} = e.target.dataset
    if(!type) return
    this.setData({
      payType:type,
      pageNo:1
    })
    this.getData()
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    const {list,allList} = this.data
    if(list.length==allList.length){
      Toast("到底了")
      return
    }
    this.filterData()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})